package com.primaryedu.ui.app.messageboard;

import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.primaryedu.R;
import com.primaryedu.bean.CommentBean;
import com.primaryedu.bean.MessagesBean;
import com.primaryedu.data.Contants;
import com.primaryedu.ui.BaseActivity;
import com.primaryedu.widget.PullToRefreshView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/9/10.
 */

public class MessagboarddetailsActivity extends BaseActivity implements View.OnClickListener ,PullToRefreshView.OnHeaderRefreshListener, PullToRefreshView.OnFooterRefreshListener {
    private TextView text_look;
    private String urlStr = "";
    private String newId = "";
    List<CommentBean> list;
    private String type = "1";//1，所有留言列表  0，五条展示
    private EditText et_messagewrite;
    private TextView text_right;
    List<MessagesBean.DataBean.RowsBean> messageslist = null;
    private int page = 1;
    private boolean fistenter = true;

    private MessagesListAdapter adapter = null;
    private Handler handler = new Handler() {

        public void handleMessage(android.os.Message msg) {

            switch (msg.what) {
                case 1001:
                    if (null != messagesbean.getData().getRows() && messagesbean.getData().getRows().size() != 0) {
                        if (messageslist == null) {
                            messageslist = messagesbean.getData().getRows();
                            adapter = new MessagesListAdapter(MessagboarddetailsActivity.this, messageslist, type);
                            listView.setAdapter(adapter);
                        }else {
                            messageslist.addAll(messagesbean.getData().getRows());
                            adapter.notifyDataSetChanged();
                        }
                    }else {
                        if( messageslist!=null && messageslist.size()>0){
                            showToast( "亲，到底了");
                        }else{
                            showToast("亲，暂无数据");
                        }
                    }
                    break;

                default:
                    break;
            }
        }

        ;
    };
    private PullToRefreshView pullToRefreshView;
    private ListView listView;
    private MessagesBean messagesbean;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_messageboarddetails);

//        urlStr = getIntent().getStringExtra("urlStr");
//        System.out.println("urlStr==" + urlStr);
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)

    @Override
    protected int BindLayout() {
        return R.layout.activity_messageboarddetails;
    }

    @Override
    protected void initView() {
        list = new ArrayList<>();
        ((TextView) findViewById(R.id.title)).setText("留言板");
        findViewById(R.id.back).setOnClickListener(this);
        listView = (ListView) this.findViewById(R.id.listView);

        pullToRefreshView = (PullToRefreshView) findViewById(R.id.main_pull_refresh_view);
        pullToRefreshView.setOnHeaderRefreshListener(this);
        pullToRefreshView.setOnFooterRefreshListener(this);
        pullToRefreshView.setLastUpdated(new Date().toLocaleString());
    }

    @Override
    protected void initData() {
        getMessagesListJson(fistenter);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onClick(View v) {
        Intent intent = null;

        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
            default:
                break;
        }
    }

    private void getMessagesListJson(boolean fistenter) {
        if (fistenter) {
            showLoading("加载中...");
        }
        StringRequest stringRequest = new StringRequest(Request.Method.POST, Contants.getMessages,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        dismissLoading();
                        System.out.println("getmessagesList==" + response);
                        Gson gson = new Gson();
                        messagesbean = gson.fromJson(response, MessagesBean.class);
                            if (messagesbean.getResult() == 1) {
                                handler.sendEmptyMessage(1001);

                            } else {
                                showToast("留言获取失败");
                            }

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                errorLoading("网络访问失败，请检查网络！");
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                //在这里设置需要post的参数
                Map<String, String> map = new HashMap<String, String>();
                map.put("pageNo", String.valueOf(page));
                map.put("pageSize", "10");
                return map;
            }
        };
        requestQueue.add(stringRequest);
    }

    @Override
    public void onFooterRefresh(PullToRefreshView view) {
        pullToRefreshView.postDelayed(new Runnable() {

            @Override
            public void run() {
//				uploadMore = true;
                page++;
                getMessagesListJson(false);
                pullToRefreshView.onFooterRefreshComplete();

            }

        }, 1500);
    }

    @Override
    public void onHeaderRefresh(PullToRefreshView view) {
        pullToRefreshView.postDelayed(new Runnable() {
            @Override
            public void run() {
//                requestMasterBoxlist(false);
                page = 1;
                adapter = null;
                messageslist = null;
                getMessagesListJson(false);
                pullToRefreshView.onHeaderRefreshComplete("更新于:"
                        + Calendar.getInstance().getTime().toLocaleString());
                pullToRefreshView.onHeaderRefreshComplete();
                Toast.makeText(MessagboarddetailsActivity.this, "数据刷新完成!", Toast.LENGTH_LONG).show();
            }

        }, 1500);
    }
}
